﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace AreaEdit.AreaSet
{
	public partial class FireZoneForm : System.Windows.Forms.Form
	{
		public FireZoneForm(int nFormLeft, int nFormTop)
		{
			this.InitializeComponent();
			this.RefreshCombobox();
			this.m_fireZoneInfo = default(FireZoneInfo);
			this.m_fireZoneInfo.m_prefix = this.tbPrefix.Text;
			this.m_fireZoneInfo.m_name = this.tbName.Text;
			this.m_fireZoneInfo.m_number = this.tbNumber.Text;
			this.m_fireZoneInfo.m_suffix = this.tbSuffix.Text;
			this.m_fireZoneInfo.m_remark = this.tbRemark.Text;
			if (!this.ReadData())
			{
				base.Location = new Point(nFormLeft, nFormTop);
				YJKMessageBox.Show("读取防火分区标注信息文件失败。");
			}
			if (this.radManualDraw.Checked)
			{
				this.RangeTypeGroupBox.Enabled = false;
			}
			this.tbPrefix.Text = "A座";
			this.tbName.Text = "防火分区";
			this.tbNumber.Text = "1";
			this.tbSuffix.Text = "";
			this.tbRemark.Text = "";
			this.isExist = true;
		}

		private void radManualDraw_Click(object sender, EventArgs e)
		{
			this.m_drawType = 0;
			this.m_clickMode = true;
			this.KeyEsc();
			this.RangeTypeGroupBox.Enabled = false;
		}

		private void radSelectRoom_Click(object sender, EventArgs e)
		{
			this.m_drawType = 1;
			this.m_clickMode = true;
			this.KeyEsc();
			this.RangeTypeGroupBox.Enabled = true;
		}

		private void radMutilSelectRoom_Click(object sender, EventArgs e)
		{
			this.m_drawType = 2;
			this.m_clickMode = true;
			this.KeyEsc();
			this.RangeTypeGroupBox.Enabled = true;
		}

		private void btnLabelSet_Click(object sender, EventArgs e)
		{
			FireZoneSetForm fireZoneSetForm = new FireZoneSetForm(this.m_labelInfo, this.m_refreshExist);
			if (DialogResult.OK == fireZoneSetForm.ShowDialog())
			{
				this.m_labelInfo = fireZoneSetForm.m_labelInfo;
				this.m_clickSetOk = true;
				this.m_refreshExist = fireZoneSetForm.m_refreshExist;
				this.KeyEsc();
			}
		}

		private void SaveData()
		{
			string userDirectoryFile = Common.GetUserDirectoryFile("\\FireZoneLabelInfo.dat");
			FileStream fileStream = new FileStream(userDirectoryFile, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_labelInfo.m_name);
			streamWriter.WriteLine(this.m_labelInfo.m_number);
			streamWriter.WriteLine(this.m_labelInfo.m_area);
			streamWriter.WriteLine(this.m_labelInfo.m_language);
			streamWriter.WriteLine(this.m_labelInfo.m_remark);
			streamWriter.WriteLine(this.m_labelInfo.m_lineWidth);
			streamWriter.WriteLine(this.m_labelInfo.m_lineColor);
			streamWriter.WriteLine(this.m_refreshExist);
			streamWriter.WriteLine(this.m_drawType);
			streamWriter.WriteLine(this.tbPrefix.Text);
			streamWriter.WriteLine(this.tbName.Text);
			streamWriter.WriteLine(this.tbNumber.Text);
			streamWriter.WriteLine(this.tbSuffix.Text);
			streamWriter.WriteLine(this.tbRemark.Text);
			streamWriter.WriteLine(this.m_isWithSpary);
			if (this.hyTextBox.Text == "∞")
			{
				streamWriter.WriteLine("0.00");
			}
			else
			{
				streamWriter.WriteLine(this.hyTextBox.Text);
			}
			streamWriter.WriteLine(this.m_wallRange);
			streamWriter.WriteLine(this.hyComboBox.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string userDirectoryFile = Common.GetUserDirectoryFile("\\FireZoneLabelInfo.dat");
			FileStream fileStream = new FileStream(userDirectoryFile, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.m_labelInfo = default(FireZoneLableInfo);
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new Point(x, y);
				this.m_labelInfo.m_name = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_number = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_area = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_language = streamReader.ReadLine();
				this.m_labelInfo.m_remark = bool.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_lineWidth = int.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_lineColor = int.Parse(streamReader.ReadLine());
				this.m_labelInfo.m_AddData.m_IsWithSpray = "WithSpary";
				this.m_labelInfo.m_AddData.m_PeopleDensity = "0.00";
				this.m_labelInfo.m_AddData.m_SelectDensityType = "人员密度(人/m^2)";
				this.m_refreshExist = bool.Parse(streamReader.ReadLine());
				this.m_drawType = int.Parse(streamReader.ReadLine());
				if (this.m_drawType == 0)
				{
					this.radManualDraw.Checked = true;
				}
				else if (1 == this.m_drawType)
				{
					this.radSelectRoom.Checked = true;
				}
				else if (2 == this.m_drawType)
				{
					this.radMutilSelectRoom.Checked = true;
				}
				string text = streamReader.ReadLine();
				string text2 = streamReader.ReadLine();
				string text3 = streamReader.ReadLine();
				string text4 = streamReader.ReadLine();
				string text5 = streamReader.ReadLine();
				string a = streamReader.ReadLine();
				if (a == "WithSpary")
				{
					this.WithSprayBtn.Checked = true;
				}
				else
				{
					this.NoSprayBtn.Checked = true;
				}
				string text6 = streamReader.ReadLine();
				if (text6 == "" || text6 == null)
				{
					text6 = "0.00";
				}
				string text7 = streamReader.ReadLine();
				if (text7 == "" || text7 == null)
				{
					text7 = "KeepWarm";
				}
				this.m_wallRange = text7;
				if (text7 == "KeepWarm")
				{
					this.KeepWarmBtn.Checked = true;
				}
				else if (text7 == "StrCenter")
				{
					this.StrCenterBtn.Checked = true;
				}
				else if (text7 == "Structure")
				{
					this.StructureBtn.Checked = true;
				}
				string text8 = streamReader.ReadLine();
				if (text8 == null)
				{
					text8 = "人员密度(人/m^2)";
				}
				if (text != "")
				{
					this.tbPrefix.Text = text;
				}
				if (text2 != "")
				{
					this.tbName.Text = text2;
				}
				if (text3 != "")
				{
					this.tbNumber.Text = text3;
				}
				if (text4 != "")
				{
					this.tbSuffix.Text = text4;
				}
				if (text5 != "")
				{
					this.tbRemark.Text = text5;
				}
				if (text6 != "")
				{
					this.hyTextBox.Text = text6;
				}
				if (text8 != "")
				{
					this.hyComboBox.Text = text8;
				}
				this.m_fireZoneInfo.m_prefix = this.tbPrefix.Text;
				this.m_fireZoneInfo.m_name = this.tbName.Text;
				this.m_fireZoneInfo.m_number = this.tbNumber.Text;
				this.m_fireZoneInfo.m_suffix = this.tbSuffix.Text;
				this.m_fireZoneInfo.m_remark = this.tbRemark.Text;
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		private void FireZoneForm_Deactivate(object sender, EventArgs e)
		{
			this.m_fireZoneInfo.m_prefix = this.tbPrefix.Text;
			this.m_fireZoneInfo.m_name = this.tbName.Text;
			this.m_fireZoneInfo.m_number = this.tbNumber.Text;
			this.m_fireZoneInfo.m_suffix = this.tbSuffix.Text;
			this.m_fireZoneInfo.m_remark = this.tbRemark.Text;
		}

		private void RefreshCombobox()
		{
			this.hyComboBox.Clear();
			string item = "人员密度(人/m^2)";
			string item2 = "人员密度(m^2/人)";
			this.hyComboBox.AddItem(item);
			this.hyComboBox.AddItem(item2);
			this.WithSprayBtn.Checked = true;
			this.radSelectRoom.Checked = true;
		}

		private void FireZoneForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void FireZoneForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void FireZoneForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.KeyEsc();
		}

		private void FireZoneForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			FireZoneForm.SendMessage(CmdFireZone._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			FireZoneForm.keybd_event(bVk, 0, dwFlags, 0);
			FireZoneForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public void SetTbNumber()
		{
			if (this.isNumber(this.tbNumber.Text))
			{
				int num = int.Parse(this.tbNumber.Text);
				this.tbNumber.Text = (num + 1).ToString();
			}
			else if (this.isAlphabet(this.tbNumber.Text))
			{
				char c = this.tbNumber.Text[0];
				c += '\u0001';
				this.tbNumber.Text = new string(c, 1);
			}
			this.m_fireZoneInfo.m_number = this.tbNumber.Text;
		}

		private bool isNumber(string str)
		{
			if (str.Length == 0)
			{
				return false;
			}
			char[] array = new char[str.Length];
			array = str.ToCharArray();
			for (int i = 0; i < str.Length; i++)
			{
				if (array[i] < '0' || array[i] > '9')
				{
					return false;
				}
			}
			return true;
		}

		private bool isAlphabet(string str)
		{
			if (1 != str.Length)
			{
				return false;
			}
			char c = str[0];
			return (c >= 'A' && c < 'Z') || (c >= 'a' && c < 'z');
		}

		private void WithSprayBtn_Click(object sender, EventArgs e)
		{
			this.m_isWithSpary = "WithSpary";
			this.m_labelInfo.m_AddData.m_IsWithSpray = this.m_isWithSpary;
		}

		private void NoSprayBtn_Click(object sender, EventArgs e)
		{
			this.m_isWithSpary = "NoWithSpary";
			this.m_labelInfo.m_AddData.m_IsWithSpray = this.m_isWithSpary;
		}

		private void KeepWarmBtn_Click(object sender, EventArgs e)
		{
			this.m_wallRange = "KeepWarm";
		}

		private void StrCenterBtn_Click(object sender, EventArgs e)
		{
			this.m_wallRange = "StrCenter";
		}

		private void StructureBtn_Click(object sender, EventArgs e)
		{
			this.m_wallRange = "Structure";
		}

		private void hyTextBox_MouseLeave(object sender, EventArgs e)
		{
		}

		private void hyTextBox_TextChanged(object sender, EventArgs e)
		{
			this.m_peopleDensity = Convert.ToDouble(this.hyTextBox.Text).ToString("f2");
			this.m_labelInfo.m_AddData.m_PeopleDensity = this.m_peopleDensity;
		}

		private void hyComboBox_textChanged(object sender, EventArgs e)
		{
			if (this.hyComboBox.Text == "人员密度(人/m^2)")
			{
				this.m_labelInfo.m_AddData.m_SelectDensityType = "人员密度(人/m^2)";
			}
			else if (this.hyComboBox.Text == "人员密度(m^2/人)")
			{
				this.m_labelInfo.m_AddData.m_SelectDensityType = "人员密度(m^2/人)";
			}
			if (this.isExist && this.m_peopleDensity != "")
			{
				this.hyTextBox.Text = (1.0 / double.Parse(this.m_peopleDensity)).ToString();
				return;
			}
			this.hyTextBox.Text = this.m_peopleDensity;
		}

		public FireZoneInfo m_fireZoneInfo;

		public FireZoneLableInfo m_labelInfo;

		public bool m_clickSetOk;

		public bool m_refreshExist;

		public bool m_clickMode;

		public int m_drawType = 1;

		public string m_isWithSpary = "WithSpary";

		public string m_wallRange = "KeepWarm";

		public string m_peopleDensity = "";

		private bool isExist;
	}
}
